﻿using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;

namespace TicketM
{
    public partial class TicketRouting : System.Web.UI.Page
    {
        private string ProcessConfigPath;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Process.GetCurrentProcess().ProcessName.ToLower().Contains("iisexpress"))
            {
                ProcessConfigPath = "/Configuration/TicketProcessingService.exe.config";
            }
            else
            {
                ProcessConfigPath = "/TicketM/Configuration/TicketProcessingService.exe.config";
            }
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(HttpContext.Current.Server.MapPath(ProcessConfigPath));
                string project = doc.DocumentElement.SelectSingleNode("/configuration/appSettings/add[@key='Project']").Attributes["value"].Value;
                Session.Add("HProject", project);
            }
            catch
            {
            }
            if (!Page.IsPostBack)
            {
                TicketM.Models.Tickets ticketingObj = new Models.Tickets();
                try
                {
                    ArrayList ProjectList = ticketingObj.GetProjects();
                    foreach (string project in ProjectList)
                    {
                        PrjLst.Items.Add(project);
                    }
                }
                catch (Exception ex)
                {
                    Logger.CreateLogEntry("!!ERROR in TicketRouting.aspx.cs while getting project list: " + ex.Message);
                    throw;
                }
            }

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow selectedRow = GridView1.SelectedRow;
            TicketTitle.Text = selectedRow.Cells[1].Text;
            SelectedTicketId.Value = selectedRow.Cells[0].Text;
        }

        protected void TicketsDS_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["sProject"] = Session["HProject"].ToString();
        }

        protected void StoreButton_Click(object sender, EventArgs e)
        {
            Models.Tickets Ticketing = new Models.Tickets();
            Ticketing.MoveTicket(PrjLst.SelectedValue, SelectedTicketId.Value);
        }

        
    }
}